package com.jjw.route.service.impl;

import com.jjw.route.config.LoginUserInterceptor;
import com.jjw.route.entity.PoiEntity;
import com.jjw.route.entity.UserEntity;
import com.jjw.route.service.*;
import com.jjw.route.to.ArticalSearchVo;
import com.jjw.route.to.ArticalTo;
import com.jjw.route.utils.R;
import com.jjw.route.vo.ArticleVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jjw.route.utils.PageUtils;
import com.jjw.route.utils.Query;

import com.jjw.route.dao.ArticalDao;
import com.jjw.route.entity.ArticalEntity;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.crypto.Data;


@Service("articalService")
public class ArticalServiceImpl extends ServiceImpl<ArticalDao, ArticalEntity> implements ArticalService {
    @Autowired
    PoiService poiService;
    @Autowired
    PortraitService portraitService;
    @Autowired
    ArticalDao articalDao;
    @Autowired
    AttentionService attentionService;

    @Autowired
    UserService userService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ArticalEntity> page = this.page(
                new Query<ArticalEntity>().getPage(params),
                new QueryWrapper<ArticalEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage1(Map<String, Object> params) {
        String key = (String)params.get("key");
        if (key!= null){
            List<Integer> userNames = new ArrayList<>();
            userNames.add(333);
            userNames.add(318);
            IPage<ArticalEntity> page = null;
            page = this.page(
                    new Query<ArticalEntity>().getPage(params),
                    new QueryWrapper<ArticalEntity>().in("uid",userNames)
            );
            List<ArticalEntity> records = page.getRecords();
            List<UserEntity> userEntities = new ArrayList<>();
            for (ArticalEntity record : records) {
                userEntities.add(userService.selectById(record.getUid()));
            }
            List<ArticleVo> articleVos = new ArrayList<>();
            for (int i = 0; i < records.size(); i++) {
                ArticleVo articleVo = new ArticleVo();
                articleVo.setId(records.get(i).getId());
                articleVo.setUid(records.get(i).getUid());
                articleVo.setImgs(records.get(i).getImgs());
                articleVo.setIlike(records.get(i).getIlike());
                articleVo.setLocation(records.get(i).getLocation());
                articleVo.setTimeinterval(records.get(i).getTimeinterval());
                articleVo.setRealsetime(records.get(i).getRealsetime().toLocaleString());
                articleVo.setName(userEntities.get(i).getName());
                articleVo.setImg(userEntities.get(i).getImg());
                articleVo.setContent(records.get(i).getContent());
                articleVos.add(articleVo);
            }
            PageUtils pageUtils = new PageUtils(page);
            pageUtils.setList(articleVos);
            return pageUtils;
        }
        String order = (String) params.get("order");
        IPage<ArticalEntity> page = null;
        if (order.equals("asc")) {
            page = this.page(
                    new Query<ArticalEntity>().getPage(params),
                    new QueryWrapper<ArticalEntity>().orderByAsc("ilike")
            );
        }else {
            page = this.page(
                    new Query<ArticalEntity>().getPage(params),
                    new QueryWrapper<ArticalEntity>().orderByDesc("ilike")
            );
        }
        List<ArticalEntity> records = page.getRecords();
        List<UserEntity> userEntities = new ArrayList<>();
        for (ArticalEntity record : records) {
            userEntities.add(userService.selectById(record.getUid()));
        }
        List<ArticleVo> articleVos = new ArrayList<>();
        for (int i = 0; i < records.size(); i++) {
            ArticleVo articleVo = new ArticleVo();
            articleVo.setId(records.get(i).getId());
            articleVo.setUid(records.get(i).getUid());
            articleVo.setImgs(records.get(i).getImgs());
            articleVo.setIlike(records.get(i).getIlike());
            articleVo.setLocation(records.get(i).getLocation());
            articleVo.setTimeinterval(records.get(i).getTimeinterval());
            articleVo.setRealsetime(records.get(i).getRealsetime().toLocaleString());
            articleVo.setName(userEntities.get(i).getName());
            articleVo.setImg(userEntities.get(i).getImg());
            articleVo.setContent(records.get(i).getContent());
            articleVos.add(articleVo);
        }
        PageUtils pageUtils = new PageUtils(page);
        pageUtils.setList(articleVos);
        return pageUtils;
    }

    @Override
    @Transactional
    public R insert(ArticalTo articalTo) {
        // 调用方法 更新流行度 平均时间
        String[] split = articalTo.getContent().split("#");
        String content = split[0];
        String poiName = split[1];
        int imgNumber = articalTo.getImgs().length;
        Integer timeInterval = articalTo.getTimeInterval();
//        SimpleDateFormat sp = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
//        String date = sp.format(new Date());
        String userName = articalTo.getUserName();
        UserEntity userEntity = userService.selectByName(userName);
        Integer uid = userEntity.getId();

        ArticalEntity articalEntity = new ArticalEntity(uid, content,
                StringUtils.join(articalTo.getImgs(),","),articalTo.getLike(),poiName,articalTo.getTimeInterval());
        articalDao.insertAndDate(articalEntity);
        // 返回新的景点信息
        PoiEntity poiEntity = poiService.poiArticalRealseUpdate(poiName, timeInterval, imgNumber);
        // 偏好度增长
        float preferenceDegree = timeInterval / poiEntity.getVtime();
        // 调用方法 更新 个人偏好
        portraitService.articalUpdatePreferenceDegree(preferenceDegree,poiEntity.getTypeid(),uid);
        return R.ok();
    }

    public ArticalEntity searchByUidTime(ArticalSearchVo articalSearchVo) {
        ArticalEntity articalEntity = articalDao.selectOne(new QueryWrapper<ArticalEntity>()
                .eq("uid", articalSearchVo.getUid())
                .eq("realsetime", articalSearchVo.getDateTime()));
        return articalEntity;
    }

    @Override
    public R like(ArticalSearchVo articalSearchVo) {
        ArticalEntity articalEntity = searchByUidTime(articalSearchVo);
        articalEntity.setIlike(articalEntity.getIlike() + 1);
        articalDao.updateById(articalEntity);
        return R.ok();
    }

    public List<ArticalEntity> getAttentionArticals() {
        int uid = LoginUserInterceptor.loginUser.get().intValue();
        List<Integer> ruids = attentionService.getAttention(uid);
        List<ArticalEntity> articalEntities = articalDao.selectList(ruids);
        return articalEntities;
    }
}